package com.touch.workshop.ui.library;
import java.net.URL;
import java.util.*;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import com.touch.workshop.ui.ImagePreview;
import com.touch.workshop.ui.TextPreview;
public class LibraryControls 
{
	public static final LibraryCollection LIBRARY = new LibraryCollection();
	public static final HashMap<String, URL> ImportLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> ScriptLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> SpriteLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> TileLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> MusicLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> SoundLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> AnimationLibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> GUILibrary = new HashMap<String, URL>();
	public static final HashMap<String, URL> DataLibrary = new HashMap<String, URL>();	
	public static final LibraryViewer Client_Library_Viewer = new LibraryViewer("Client");
	public static final LibraryViewer Server_Library_Viewer = new LibraryViewer("Server");
	public static void preview(final String name, final URL url)
	{
		try
		{
			final String type = url.openConnection().getContentType(); 
			if(type.contains("image"))
				new ImagePreview(new ImageIcon(url), name);
			else if(type.contains("text"))
				new TextPreview(name, url.openStream());
			else
				JOptionPane.showMessageDialog(null, "Unknown type: " + type);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	protected LibraryControls() {}
	public static void importFile(String name, DefaultMutableTreeNode parent, URL fileURL)
	{
		final TreeNode path[] = parent.getPath();
		
		ImportLibrary.put(name, fileURL);
		
		for(TreeNode p: path)
		{
			final String parentName = p.toString();		
			if(parentName.equals("Sprites"))
			{
				SpriteLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Animations"))
			{
				AnimationLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Tiles"))
			{
				TileLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Sound Effects"))
			{
				SoundLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Music"))
			{
				MusicLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Scripts"))
			{
				ScriptLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("Data"))
			{
				ScriptLibrary.put(name, fileURL);
				return;
			}
			else if(parentName.equals("GUI"))
			{
				ScriptLibrary.put(name, fileURL);
				return;
			}
		}
	}
}
